home *** CD-ROM | disk | FTP | other *** search
- Path: news.mira.net.au!news
- From: davidw@werple.net.au (David White)
- Newsgroups: comp.lang.c++
- Subject: Re: Recursive CONSTRUCTOR allowed?
- Date: 17 Apr 1996 20:29:27 +1000
- Organization: Werple Internet, Melbourne
- Message-ID: <4l2h67$dfd@werple.net.au>
- References: <4l1ck5$pit@falcon.ccs.uwo.ca>
- NNTP-Posting-Host: werplez.mira.net.au
-
- Sharon Wang <swang1@julian.uwo.ca> writes:
-
- >I'd like to implement the following algorithm using constructor
- >(I am TOLD to do so)
-
- >// create an n linked list
- >create node(n)
- >{
- > if (n = 0) then
- > reach the length, don't create!
- > else
- > create this node;
- > create node(n-1);
- > make link;
- > end if
- > return;
- >}
-
- >(1) FIrst of all, does this make sense?
-
- I think so.
-
- >(2) Is is possible to use constructor instead of member function?
-
- Yes, if I understand the problem correctly.
-
- >Any suggestion + plus small piece of code is highly appreciated.
-
- class Node
- {
- public:
- Node(int n);
- //...
-
- private:
- Node *next;
- };
-
- Node::Node(int n)
- {
- next = (n > 0) ? new Node(n-1) : 0;
- }
-
- void f()
- {
- Node list(3);
- }
-
- Here, 'f' creates a forward-linked list of four nodes. Strictly speaking,
- the constructor isn't recursive because it is being called for a different
- object each time, but it does recursively create new objects.
-
- >Sharon
- ><swang1@julian.uwo.ca>
-
- David White
- davidw@werple.mira.net.au
-